package com.gin.kotlin.no_gay_alliance.adapter.vote

import android.content.Context
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemOptionScoreBinding
import com.gin.nga.response.vote.ScoreResult
import java.util.Locale

class VoteScoreAdapter(context: Context, handler: MyHandler, data: MutableList<ScoreResult> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemOptionScoreBinding, ScoreResult>(context, handler, true, data) {
    override fun onBindViewHolder(holder: ViewHolder<ItemOptionScoreBinding>, position: Int, b: ItemOptionScoreBinding, dataModel: ScoreResult) {
        // 总人数
        val total = data.sumOf { it.count }
        val max = dataModel.score * 2
        val min = max - 1
        val percent = String.format(Locale.CHINESE, "%.2f", 100.0 * dataModel.count / total)

        b.tvTitle.text = "${min}分 ~ ${max}分"
        b.tvCount.text = "${dataModel.count}人 (${percent}%)"
        b.progressBar.max = total
        b.progressBar.setProgress(dataModel.count, true)
    }
}